//
//  ChatMessageCell.m
//  搭建框架(代码已封装)
//
//  Created by My Mac on 2017/2/3.
//  Copyright © 2017年 My Mac. All rights reserved.
//

#import "ChatMessageCell.h"
#import "ChatMessages.h"
#import "ChatMessageCellFrame.h"
/*
 添加三个控件，在自定义cell里面放置
 时间、头像、正文
 */
@interface ChatMessageCell()

@property(nonatomic,weak) UILabel *time;
@property(nonatomic,weak) UIImageView *icon;
@property(nonatomic,weak) UIButton *textBtn;

@end

@implementation ChatMessageCell
//自定义cell必须重写自定义cell的initWithStyle：方法
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        //Time
        UILabel * timeLable = [[UILabel alloc]init];
        timeLable.font = [UIFont systemFontOfSize:12];
        timeLable.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:timeLable];
        _time = timeLable;
        
        //icon
        UIImageView * imgView = [[UIImageView alloc]init];
        [self.contentView addSubview:imgView];
        _icon = imgView;
        
        //Text
        UIButton * button = [[UIButton alloc]init];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        button.titleLabel.numberOfLines = 0;
        button.contentEdgeInsets = UIEdgeInsetsMake(5, 10, 5, 10);
        [self.contentView addSubview:button];
        _textBtn = button;
    }
    self.backgroundColor = [UIColor clearColor];
    return self;
    
}
-(void)setCellFrame:(ChatMessageCellFrame *)cellFrame
{
    _cellFrame = cellFrame;
    //获取数据模型
    ChatMessages *messages = cellFrame.message;
    
    //设置数据和frame
    _time.text = messages.time;
    _time.frame = _cellFrame.timeFrame;
    
    _icon.image = (messages.type == ChatMessagesTypeMe)?([UIImage imageNamed:@"me"]):([UIImage imageNamed:@"other"]);
    _icon.frame = _cellFrame.iconFrame;
    
    [_textBtn setTitle:messages.text forState:UIControlStateNormal];
    _textBtn.frame = _cellFrame.textBtnFrame;
    //设置正文的背景
    NSString * img,*hightImg;
    if(messages.type == ChatMessagesTypeMe){
        img = @"chat_send_nor";
        hightImg =@"chat_send_press_pic";
        [_textBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    } else {
        img = @"chat_recive_nor";
        hightImg = @"chat_recive_press_pic";
        [_textBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    //正文的size来修改气泡图片防止变形
    UIImage * imgNor = [UIImage imageNamed:img];
    UIImage * imgHig = [UIImage imageNamed:hightImg];
    //以平铺的方式拉伸图片（4个角分开弄）
    imgNor = [imgNor stretchableImageWithLeftCapWidth:imgNor.size.width*0.5 topCapHeight:imgNor.size.height*0.5];
    imgHig = [imgHig stretchableImageWithLeftCapWidth:imgHig.size.width*0.5 topCapHeight:imgHig.size.height*0.5];
    
    [_textBtn setBackgroundImage:imgNor forState:UIControlStateNormal];
    [_textBtn setBackgroundImage:imgHig forState:UIControlStateHighlighted];
    
    
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
